package com.longcai.piaojia.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.EvaluateListGet;
import com.longcai.piaojia.widget.GoodsEvaluateListView;
import com.longcai.piaojia.widget.GoodsEvaluateView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;

/**
 * Created by Administrator on 2015/6/1.
 */
public class GoodsEvaluateListAdapter extends ArrayAdapter<EvaluateListGet.EvaluateEntity> {

    private LayoutInflater layoutInflater;
    private Context context;
    private EvaluateZanListener evaluateZanListener;

    public GoodsEvaluateListAdapter(Context context, List<EvaluateListGet.EvaluateEntity> objects, EvaluateZanListener evaluateZanListener) {

        super(context, 0, objects);

        this.context = context;

        this.evaluateZanListener = evaluateZanListener;

        layoutInflater = ((Activity)context).getLayoutInflater();

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;

        EvaluateListGet.EvaluateEntity evaluateEntity = getItem(position);

        if(convertView == null){

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_goods_evaluate_list, null)));

            convertView.setTag(viewHolder);

        }else{

            viewHolder = (ViewHolder) convertView.getTag();

        }

        if(position==0){

            viewHolder.item_goods_evaluate_list_line_v.setVisibility(View.GONE);

        }else{

            viewHolder.item_goods_evaluate_list_line_v.setVisibility(View.VISIBLE);

        }

        viewHolder.item_goods_evaluate_list_star_tv.setSelect(Integer.parseInt(evaluateEntity.star));

        viewHolder.item_goods_evaluate_list_title_tv.setText(evaluateEntity.nickname);

        viewHolder.item_goods_evaluate_list_content_tv.setText(evaluateEntity.content);

        viewHolder.item_goods_evaluate_list_time_tv.setText(evaluateEntity.datetime);

        viewHolder.item_goods_evaluate_list_number_tv.setText(evaluateEntity.zambia);

        if(evaluateEntity.zambia_state.equals("1")){

            viewHolder.item_goods_evaluate_list_zan_iv.setImageResource(R.mipmap.evaluate_sel);

        }else{

            viewHolder.item_goods_evaluate_list_zan_iv.setImageResource(R.mipmap.evaluate_no);

        }

        viewHolder.item_goods_evaluate_list_zan_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                evaluateZanListener.setZan(position);

            }
        });

        GlideLoader.getInstance().get(context, evaluateEntity.avatar, viewHolder.item_goods_evaluate_list_iv, R.mipmap.mine_headimg, GlideLoader.TYPE_IMAGE_CIRCLE);

        return convertView;

    }

    private class ViewHolder{

        @BoundView(R.id.item_goods_evaluate_list_line_v) public View item_goods_evaluate_list_line_v;
        @BoundView(R.id.item_goods_evaluate_list_iv) public ImageView item_goods_evaluate_list_iv;
        @BoundView(R.id.item_goods_evaluate_list_star_tv) public GoodsEvaluateListView item_goods_evaluate_list_star_tv;
        @BoundView(R.id.item_goods_evaluate_list_title_tv) public TextView item_goods_evaluate_list_title_tv;
        @BoundView(R.id.item_goods_evaluate_list_content_tv) public TextView item_goods_evaluate_list_content_tv;
        @BoundView(R.id.item_goods_evaluate_list_time_tv) public TextView item_goods_evaluate_list_time_tv;
        @BoundView(R.id.item_goods_evaluate_list_number_tv) public TextView item_goods_evaluate_list_number_tv;
        @BoundView(R.id.item_goods_evaluate_list_zan_ll) public LinearLayout item_goods_evaluate_list_zan_ll;
        @BoundView(R.id.item_goods_evaluate_list_zan_iv) public ImageView item_goods_evaluate_list_zan_iv;

    }

    public interface EvaluateZanListener{

        void setZan(int pos);

    }

}
